package jzqb.service.impl;


import haha.life.common.BelongToSqlUtils;
import haha.life.normal.FileuploadUtils;
import jzqb.bo.AdminQueryBo;
import jzqb.dao.AdminUserInfoDao;
import jzqb.dao.ZYDao;
import jzqb.po.AdminUserPo;
import jzqb.po.ZYPo;
import jzqb.service.AdminZYService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 自用库业务层实现类
 */
@Service
public class AdminZYServiceImpl implements AdminZYService {

    @Autowired
    private ZYDao zyDao;

    @Autowired
    private AdminUserInfoDao adminUserInfoDao;

    @Override
    public Boolean insertZY(ZYPo po) {
        String year = FileuploadUtils.getYear(po.getPublishDate());
        po.setYear(year);
        return zyDao.insertZY(po);
    }

    @Override
    public Boolean updateZY(ZYPo po) {
        String year = FileuploadUtils.getYear(po.getPublishDate());
        po.setYear(year);
        return zyDao.updateZY(po);
    }

    @Override
    public Boolean deleteZY(ZYPo zyPo) {
        return zyDao.deleteZY(zyPo);
    }

    @Override
    public List<ZYPo> queryZY(AdminQueryBo bo) {
        String[] standardType = bo.getStandardType();
        String type ="";
        List<AdminUserPo> allUserList = null;//全部用户
        List<ZYPo>  list = null;
        if(standardType.length == 8){
            type = null;
        }else{
            type = BelongToSqlUtils.getBelongToSql(standardType, "领域");
        }
        bo.setType(type);
        list = zyDao.queryZY(bo);
        if (list != null && !list.isEmpty()) {
            allUserList = adminUserInfoDao.findAllUser();
            for (ZYPo po : list) {
                if (allUserList != null && !allUserList.isEmpty()) {
                    for (AdminUserPo userPo : allUserList) {
//                        po.setRecorderName(po.getRecorder());
                        if(po.getRecorder().equals(userPo.getId())){
                            po.setRecorderName(userPo.getName());
                            break;
                        }
                    }
                }
            }
        }
        return list;
    }

    @Override
    public Long count(AdminQueryBo bo) {
        return zyDao.count(bo);
    }

}

